<template>
	<view>
		<mescroll-uni :ref="'mescrollRef'+i" @init="mescrollInit" @down="downCallback"
			@up="upCallback">
			<view>
				<view v-for="(item, index) in dataList" :key="index" class="u-m-b-6">
					<view style="padding: 20rpx 36rpx 26rpx;background-color: #FFFFFF;">
						<view class="u-p-b-16 u-m-b-18 u-f-28 u-lh-40 font-medio flex-center-between" style="border-bottom: 2rpx solid #eee;">
							<view>{{ lang_shop.reverseDate }}: {{ dayjs(item.reverseDate).format('YYYY/MM/DD') }} {{ subscribeDay(item.reverseDate) }}</view>
							<view class="color-red">{{ formatStatusText(item.reverseStatus) }}</view>
						</view>
						<view class="u-p-b-16 u-m-b-20 u-f-28 u-lh-40 flex" style="border-bottom: 2rpx solid #eee;">
							<view class="clear-flex" style="width: 136rpx; height: 136rpx; background-color: #f9f9f9; overflow: hidden;">
								<x-image :src="showImage(shopInfo.logoImgUrl, 'logo')"/>
							</view>
							<view class="u-m-l-16 flex-column flex-between">
								<view class="u-f-28 u-lh-40 font-medio">{{ item.merchantServiceTitle || '' }}</view>
								<view class="flex-center u-f-26 u-lh-36">
									<view>{{ lang_comerciante.people_number }}: {{ item.amount || 0 }}{{ lang_unit.people }}</view>
									<view style="margin: 0 16rpx;height: 18rpx; width: 2rpx;background-color: #999;"></view>
									<view>{{ item.merchantStaffName || '' }}</view>
								</view>
								<view class="u-f-26 u-lh-36 color-grew">{{ lang_extension.contactNumber }}: {{ item.phone || '' }}</view>
							</view>
						</view>
						<view class="flex-center-between">
							<view></view>
							<view class="flex-center">
								<view class="u-m-l-16">
									<x-common-btn
									width="150"
									height="56"
									background="#FFFFFF"
									color="#333333"
									borderColor="#E9E9E9"
									@click="$emit('phone', item)"
									>
										<view class="u-f-24 u-lh-34 font-medio">{{ lang_shop.instantCommunication }}</view>
									</x-common-btn>
								</view>
								<template v-if="item.reverseStatus === 0">
									<view class="u-m-l-16">
										<x-common-btn
										width="150"
										height="56"
										background="#FFFFFF"
										color="#333333"
										borderColor="#E9E9E9"
										@click="subscirbeAction(item, -2)"
										>
											<view class="u-f-24 u-lh-34 font-medio">{{ lang_shop.rejectSubscribe }}</view>
										</x-common-btn>
									</view>
	
									<view class="u-m-l-16">
										<x-common-btn
										width="150"
										height="56"
										@click="subscirbeAction(item, 1)"
										>
											<view class="u-f-24 u-lh-34 font-medio">{{ lang_shop.resolveSubscribe }}</view>
										</x-common-btn>
									</view>
								</template>
								<template v-if="item.reverseStatus === 1">
									<view v-if="showSignBtn(item)" class="u-m-l-16">
										<x-common-btn
										width="150"
										height="56"
										background="#FFFFFF"
										color="#333333"
										borderColor="#E9E9E9"
										@click="subscirbeAction(item, -3)"
										>
											<view class="u-f-24 u-lh-34 font-medio">{{ lang_shop.signSubscribe }}</view>
										</x-common-btn>
									</view>
									<view class="u-m-l-16">
										<x-common-btn
										width="150"
										height="56"
										@click="subscirbeAction(item, 2)"
										>
											<view class="u-f-24 u-lh-34 font-medio">{{ lang_shop.completeSubscribe }}</view>
										</x-common-btn>
									</view>
								</template>
							</view>
						</view>
					</view>
				</view>
			</view>
		</mescroll-uni>
	</view>
</template>

<script>
	import dayjs from 'dayjs'
	import { merchantServiceReverseRecordPage } from '@/api/merchant/service.js'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import MescrollMoreItemMixin from "@/components/mescroll-uni/mixins/mescroll-more-item.js";

	export default {
		mixins: [MescrollMixin, MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
		props: {
			i: Number, // 每个tab页的专属下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
			index: { // 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
				type: Number,
				default () {
					return 0
				}
			},
			list: {
				default: () => ([])
			},
			reverseId: {
				default: ''
			},
			statusObj: {
				default: () => ({})
			}
		},
		data() {
			return {
				dataList: [],
				visible: false
			}
		},
		watch: {
			reverseId: {
				handler(v) {
					if (!v) {
						this.downCallback()
					}
				},
				deep: true,
				immediate: true
			}
		},
		methods: {
			// 当前星期几
			subscribeDay(reverseDate) {
				// 初始化周期为7
				const DAY = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
				return this.lang_time[DAY[dayjs(dayjs(reverseDate || dayjs())).day()]] || ''
			},
			formatStatusText(status = 0) {
				let item = [...this.list, { title: this.lang_shop.broken, value: -3 }].find(item => item.value === status) || {}
				return item.title || ''
			},
			// 判断是否到达显示时间
			showSignBtn(item) {
				return dayjs(item.reverseDate).add(1, 'day').valueOf() < dayjs().valueOf(0)
			},
			// 触发提交表单
			subscirbeAction({ reverseId }, status) {
				this.$emit('change', { reverseId, status })
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				let node = this.$refs['mescrollRef' + this.i]
				let queryObj = { pageNumber: page.num, pageSize: page.size }
				let { value } = this.statusObj
				if (typeof value === 'number') {
					queryObj.reverseStatus = value
				}
				let getList = () => merchantServiceReverseRecordPage({ ...queryObj });
				getList().then(res => {
					node && node.mescroll.endSuccess(res.list.length);
					if (page.num == 1) this.dataList = []; //如果是第一页需手动制空列表
					this.dataList = this.dataList.concat(res.list); //追加新数据
				}).catch(() => {
					node && node.mescroll.endErr();
				})
			},
			downCallback() {
				let node = this.$refs['mescrollRef' + this.i]
				node && node.mescroll.resetUpScroll()
			},
		}
	}
</script>
